﻿
using LS.Nc6.Core;
using Microsoft.Extensions.Options;

namespace LS.Nc6.Application.Cms
{
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    [ApiDescriptionSettings("CMS")]
    public class ChannelService : IDynamicApiController
    {
        private readonly ISysUserService _userService;

        private readonly IHttpContextAccessor _httpContextAccessor;

        private readonly IEventPublisher _eventPublisher;

        private readonly ISysConfigurationService _configurationService;

        private readonly ICmsChannelService _channelService;

        private readonly ISysFileService _fileService;

        private readonly ICache _cache;
        public ChannelService(ISysUserService userService, IEventPublisher eventPublisher, ISysConfigurationService configurationService, IHttpContextAccessor httpContextAccessor, 
            ICmsChannelService channelService, ISysFileService sysFileService,
            IOptions<CacheOptions> cacheOptions, Func<string, ISingleton, object> resolveNamed)
        {
            _userService = userService;
            _httpContextAccessor = httpContextAccessor;
            _eventPublisher = eventPublisher;
            _configurationService = configurationService;
            _channelService = channelService;
            _cache = resolveNamed(cacheOptions.Value.CacheType.ToString(), default) as ICache;
            _fileService = sysFileService;
        }

        /// <summary>
        /// 获取栏目Tree格式返回
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("api/channel/tree")]
        public async Task<List<ChannelTreeOutput>> GetChannelTree()
        {
            List<ChannelTreeOutput> lsetm = new List<ChannelTreeOutput>();
            if (_cache.Get<List<ChannelTreeOutput>>("ApiChannelData") != null && _cache.Get<List<ChannelTreeOutput>>("ApiChannelData").Count > 0)
            {
                lsetm = _cache.Get<List<ChannelTreeOutput>>("ApiChannelData");
            }
            else
            {
                List<CmsChannelEntity> channellist = await _channelService.List(a => true, a => a.Taxis);
                if (channellist != null)
                {
                    var channelhomelist = channellist.Where(w => w.ParentId == 0);
                    foreach (var item in channelhomelist)
                    {
                        ChannelTreeOutput etm = new ChannelTreeOutput();
                        etm.Id = item.Id;
                        etm.Name = item.ChannelName;
                        etm.IndexName = item.IndexName;
                        GetChildren(etm, channellist, item.Id);
                        lsetm.Add(etm);
                    }
                }
                await _cache.SetAsync("ApiChannelData", lsetm);
            }
            return lsetm;
        }

        /// <summary>
        /// 获取栏目详情
        /// </summary>
        /// <param name="id">栏目id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/channel/detail")]
        public async Task<ChannelModelOutPut> GetModel(long id)
        {
            var model = await _channelService.GetByIdAsync(id);
            ChannelModelOutPut channelmodel = model.Adapt<ChannelModelOutPut>();
            var filelist =await _fileService.CustomerFileList(model.ImageUrlIds);
           var urllist = new List<string>();
            foreach (var file in filelist)
            {
                
                urllist.Add(App.HttpContext.Request.Host.ToString()+file["fpath"]);
                
            }
            channelmodel.ImageUrls = urllist;
            return  channelmodel;
        }

        #region 私有方法
        private void GetChildren(ChannelTreeOutput etm, List<CmsChannelEntity> listpartmenu, long pid)
        {
            var children = listpartmenu.Where(w => w.ParentId == pid);
            List<ChannelTreeOutput> lsetm = new List<ChannelTreeOutput>();
            if (children != null && children.Count() > 0)
            {
                foreach (var item in children)
                {
                    ChannelTreeOutput etmchildren = new ChannelTreeOutput();
                    etmchildren.Id = item.Id;
                    etmchildren.Name = item.ChannelName;
                    etmchildren.IndexName = item.IndexName;
                    GetChildren(etmchildren, listpartmenu, item.Id);
                    lsetm.Add(etmchildren);
                }
                if (lsetm.Count > 0)
                {
                    etm.Children = lsetm;
                }
            }
        }


        #endregion

    }
}
